<?php
require_once(ROOT_PATH ."/includes/payments/epayunionpay/sdk/acp_service.php");

class EpayunionpayPayment extends BasePayment {


    function get_payform($order_info) {
        $order_info=$this->get_order_info($order_info);
        
        if(!$order_info){
            return false;
        }

        $frontUrl = $this->_create_return_url($order_info['order_id']);
        $backUrl = site_url() . "/notifyunionpay.php";

        $orderId = $order_info['order_sn'];
        $txnTime = date('YmdHis');
        $txnAmt = intval(bcmul($order_info['money'],100 ,0));

        $params = array(
            //以下信息非特殊情况不需要改动
            'version' => '5.0.0', //版本号
            'encoding' => 'utf-8', //编码方式
            'txnType' => '01', //交易类型
            'txnSubType' => '01', //交易子类
            'bizType' => '000201', //业务类型
            'frontUrl' => $frontUrl, //前台通知地址
            'backUrl' => $backUrl, //后台通知地址
            'signMethod' => '01', //签名方法
            'channelType' => '08', //渠道类型，07-PC，08-手机
            'accessType' => '0', //接入类型
            'currencyCode' => '156', //交易币种，境内商户固定156
            //TODO 以下信息需要填写
            'merId' => SDK_SIGN_MERID, //商户代码，请改自己的测试商户号，此处默认取demo演示页面传递的参数
            'orderId' => $orderId, //商户订单号，8-32位数字字母，不能含“-”或“_”，此处默认取demo演示页面传递的参数，可以自行定制规则
            'txnTime' => $txnTime, //订单发送时间，格式为YYYYMMDDhhmmss，取北京时间，此处默认取demo演示页面传递的参数
            'txnAmt' => $txnAmt, //交易金额，单位分，此处默认取demo演示页面传递的参数
// 		'reqReserved' =>'透传信息',        //请求方保留域，透传字段，查询、通知、对账文件中均会原样出现，如有需要请启用并修改自己希望透传的数据
                //TODO 其他特殊用法请查看 special_use_purchase.php
        );

        AcpService::sign($params, SDK_SIGN_CERT_PATH);


        $html_form = AcpService::createAutoFormHtml($params, SDK_FRONT_TRANS_URL);
        echo $html_form;
        exit();
    }

    function _create_return_url($order_id) {
        return site_url() . "/index.php?app=buyer_order";
    }

    function verify_notify() {
//        $data = serialize($_POST);
//        file_put_contents('1.txt', $data."--".date('Y-m-d H:i:s'));
//$str='a:23:{s:5:"accNo";s:16:"6221********0000";s:10:"accessType";s:1:"0";s:7:"bizType";s:6:"000201";s:6:"certId";s:11:"68759585097";s:12:"currencyCode";s:3:"156";s:8:"encoding";s:5:"utf-8";s:5:"merId";s:15:"700000000000001";s:7:"orderId";s:18:"201603301543073949";s:7:"queryId";s:21:"201603301543071304178";s:8:"respCode";s:2:"00";s:7:"respMsg";s:8:"Success!";s:9:"settleAmt";s:1:"1";s:18:"settleCurrencyCode";s:3:"156";s:10:"settleDate";s:4:"0330";s:10:"signMethod";s:2:"01";s:7:"traceNo";s:6:"130417";s:9:"traceTime";s:10:"0330154307";s:6:"txnAmt";s:1:"1";s:10:"txnSubType";s:2:"01";s:7:"txnTime";s:14:"20160330154307";s:7:"txnType";s:2:"01";s:7:"version";s:5:"5.0.0";s:9:"signature";s:344:"SdZHSlY1omfcMFavrDbVcYFqGtTV0tJgoTi0tRNja9w5sG6t3faz9nS4/jYA/H+iwqqv9MVvg9AibBZJCunYggPW1TFPfLVatncm/29vXbxtCQKligfJ2UY/Ws94AVm9gY+m7i2tsHw9LG8HB6sSdwi+1Zigtg+Txx/3Avr5CekVOeNsIwxgtl7HCL3BVH2YJw3RDP/DKsTQ/5UTtKzxqE5BbxXvE0ZNYFtQvolRfJa7qlujYS8zBDXQ2k5kNAs2h5QICV/DFV4GkO9eUHpuEqD8Bh/MlwWc1zer26J+DS0Dky4KHXRmE5056fMI6oRjdmqPGKiV/1it08euMq82EA==";}';
//$_POST = unserialize($str);

        if (isset($_POST ['signature'])) {
            if (AcpService::validate($_POST)) {
                $notify_result = array(
                    'out_trade_no' => $_POST['orderId'], #商户订单号
                    'trade_no' => $_POST['orderId'], #交易凭据单号
                    'total_fee' => $_POST['txnAmt'] / 100, #涉及金额
                    'trade_status' => '1',
                );
            } else {
                $notify_result = array(
                    'trade_status' => '0',
                );
            }
        } else {
            //签名为空
            $notify_result = array(
                'trade_status' => '0',
            );
        }
        return $notify_result;
    }

    /**
     * 此处略过检查
     */
    function verify_return() {
        if (isset($_POST ['orderId'])) {
            $out_trade_no = $_POST['orderId']; //商户订单号
            $order_mod = &m('order');
            $order = $order_mod->get('order_sn=' . $out_trade_no);
            if ($order) {
                header('Location: index.php?app=buyer_order&act=view&order_id=' . $order['order_id']);
            } else {
                header('Location: index.php?app=epay&act=logall');
            }
        } else {
            header('Location: index.php?app=member');
        }
    }

}

?>